package com.prodog.gamemodule.fight.fightobj.builder.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.prodog.commonmodule.prop.caculate.factory.ObjPropHandlerFactory;
import com.prodog.commonmodule.prop.entity.ObjProp;
import com.prodog.gamemodule.fight.effect.entity.FightEffect;
import com.prodog.gamemodule.fight.fightobj.builder.FightObjBuilder;
import com.prodog.gamemodule.fight.fightobj.entity.FightObj;
import com.prodog.gamemodule.fight.fightobj.entity.FightSkill;
import com.prodog.usermodule.role.entity.RoleInfo;
import com.prodog.usermodule.role.entity.RoleProp;
import com.prodog.usermodule.role.wrapper.RoleInfoWrapper;
import com.prodog.usermodule.role.wrapper.RolePropWrapper;
import com.prodog.utils.bean.BeanUtil;
import com.prodog.utils.interfaces.AutowiredBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashMap;

public class RoleInfoFightObjBuilder implements FightObjBuilder<RoleInfo>, AutowiredBean {
    @Autowired
    private RoleInfoWrapper roleInfoWrapper;
    @Autowired
    private RolePropWrapper rolePropWrapper;


    public RoleInfoFightObjBuilder() {
        this.autowired();
    }

    @Override
    public FightObj build(RoleInfo roleInfo) {
        RoleProp roleProp = rolePropWrapper.getById(roleInfo.getId());

        FightObj fightObj = new FightObj() {{
            setName(roleInfo.getRoleName());
            setPanelProp(new ObjPropHandlerFactory().getHandler(RoleProp.class, new HashMap() {{
                put(ObjPropHandlerFactory.KEY_ROLE_INFO, roleInfo);
            }}).handle(roleProp));
            setFightProp(BeanUtil.beanToBean(getPanelProp(), ObjProp.class));
            setUuid(IdUtil.simpleUUID());
            setFlag(0);
            setId(roleInfo.getId());
            getSkills().put("1", new FightSkill() {{
                setId("1");
                setName("剑击");
                setLevel(1);
                setNormal(true);
                setDamageType(1);
                setTargetNums(1);
                setSkillType(1);
                setExpression("parseInt(${obj.physicalAttack} * (${physicalAttackPercent}*0.01))");
                //setFightAi("com.prodog.gamemodule.fight.ai.impl.NormalFightAI");
                /*setAffectType(2);
                setAffectTeam(true)？？/?asads;*/
                setProp(new HashMap() {{
                    put("physicalAttackPercent", 200);
                }});
            }});
            getSkills().put("2", new FightSkill() {{
                setId("2");
                setName("雷神之息");
                setLevel(1);
                setSkillType(3);
                setProp(new HashMap() {{
                    put("targetNums", 3);
                    put("upgradePercent", 50);
                    put("paralysisRate", 100);
                    put("paralysisRound", 3);
                }});
                setEffects(Arrays.asList("test"));
            }});
        }};
        for (FightSkill skill : fightObj.getSkills().values()) {
            if (CollUtil.isNotEmpty(skill.getEffects())) {
                for (String effect : skill.getEffects()) {
                    fightObj.getEffects().add(new FightEffect() {{
                        setName(effect);
                        setParams(skill.getProp());
                        getParams().put("skillId", skill.getId());
                    }});
                }
            }
        }
        return fightObj;
    }
}
